// JavaScript Document
// kusht.js
// Here we have the client-side model

// This is the big important variable that holds 
// all of the client's game-state information that 
// he's allow to have access to
var client_state = {};

var last_room_name;
function clientUpdate() {
	// Equivalent to "autolook"
	if(client_state.in_room.name!=last_room_name) {
		look();
		last_room_name = client_state.in_room.name;
	}
}

var CMDLIST = ["look"];


// Found out if it's a proper client-side command
// What are client side commands? Any information
// that the character would be able to find by a simple
// lookup operation --> Any data-fetching command is local
var CMDLIST = ["look"];
var command = {};
command['look'] = look;

function cmd_handler(msg) {
	cmdWord = msg.substring(0,msg.search(" "));
	if (cmdWord=="") { cmdWord = msg; }
	cmd = "";
	
	if(msg=="") {
		return;
	}
	
	cmdWord = new RegExp(["^" + cmdWord.toLowerCase()]);
	
	for(i in CMDLIST) {
		if (CMDLIST[i].match(cmdWord)) {
			cmd = CMDLIST[i];
			break;
		}
	}
	
	if (cmd=="") {
		// It's probably a real system command!
		// (or junk)
		try{ socket.send(msg); } catch(ex){ log(ex); }
		return;
	}
	
	// Call the function!
	command[cmd]();
	
	//var RegExp
	//split
}

function look() {
	log("<div id='room_img'><img id='room_img_act' src=images/"+client_state.in_room.image+" /></div>");
	log("<div id='room_name'>"+client_state.in_room.name+"</div>");
	log("<div id='room_desc'>"+client_state.in_room.description+"</div>");
	// Exits
	log("<div id='room_exit'>Obvious Exits:&nbsp;</div>");
	for(i in client_state.in_room.exits) {
		log("<div id='room_exit'>"+" ["+client_state.in_room.exits[i].command+"] "+client_state.in_room.exits[i].name+"&nbsp;</div>");
		if (i>0) { log(", "); }
	}
	log("<br style='clear:left' />");
	
	for(i in client_state.in_room.people) {
		log("<div id='char_in_room_img'><img id='char_in_room_img_act' src=images/" + client_state.in_room.people[i].image + " /></div>");
		log("<div id='char_in_room_name'>"+client_state.in_room.people[i].name+"</div>");
	}
}